package com.hua.api.modules.pay.util;

import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
import com.hua.api.core.util.StringUtil;
import com.hua.api.modules.pay.domain.OrderInfoPayinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@Slf4j
@Configuration
@EnableConfigurationProperties()
public class WechatPayUtil {

    // 退款回调
    @Value("${wechat.refundNotifyUrl}")
    private String refundNotifyUrl;


    // 元转分
    private  Integer yuanTofen(Double payMoney){
        payMoney = payMoney*100d;
        return payMoney.intValue();
    }


    public  WxPayRefundV3Request getWxPayRefundV3Request(OrderInfoPayinfo payinfo){
        WxPayRefundV3Request request = new WxPayRefundV3Request();
        request.setOutTradeNo(payinfo.getOutTradeNo());
        request.setOutRefundNo(payinfo.getOutRefundNo());
        WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
        amount.setCurrency("CNY");
        amount.setTotal(yuanTofen(payinfo.getPayMoney()));
        amount.setRefund(yuanTofen(payinfo.getRefundMoney()));
        request.setAmount(amount);
        request.setReason(payinfo.getRefundReson());
        request.setNotifyUrl(refundNotifyUrl);
        return request;
    }



    public  String createWechatOutTradeNo(int lenth){
        return "985"+StringUtil.createFullId(lenth);
    }

    public  String createWechatOutRefundNo(int lenth){
        return "211"+StringUtil.createFullId(lenth);
    }

}
